package com.ddwl.goods.service;

import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.goods.dao.CategoryDao;
import com.ddwl.goods.model.Category;
import com.ddwl.schema.bo.goods.CategorySearchBo;
import com.ddwl.schema.vo.goods.CategoryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Admin
 */
@Service
@RequiredArgsConstructor
public class MallCategoryService {

    private final CategoryDao categoryDao;

    public List<CategoryVo> indexMall() {
        List<Category> entities = categoryDao.findTopMall();
        List<CategoryVo> list = entities.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            //查询多级子分类（这里只考虑二级分类）
            List<Category> chrilList = categoryDao.findChildrenList(category.getId());
            List<CategoryVo> chrilListVo = BeanConverUtils.convertBeanList(chrilList, CategoryVo.class);
            categoryVo.setChildren(chrilListVo);
            return categoryVo;
        }).collect(Collectors.toList());

        return list;
    }

    public List<CategoryVo> searchMallChildren(CategorySearchBo bo) {
        List<Category> childrenList = categoryDao.findChildrenList(bo.getId());
        return childrenList.stream().map(this::convert).collect(Collectors.toList());
    }

    private CategoryVo convert(Category entity) {
        CategoryVo vo = new CategoryVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
